﻿using Microsoft.SharePoint;
using Microsoft.SharePoint.Publishing;
using ppscd.Configuration;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ppscd
{
    public class SharepointHandler
    {

        static SharepointHandler mHandler;

        private string mWelcomePage = "";
        private List<SPListItem> sharepointItems = new List<SPListItem>();
        string destinationServer = "";
        string siteCollectionPath = "";

        public static SharepointHandler Current
        {
            get
            {
                if (mHandler == null)
                {
                    mHandler = new SharepointHandler(PerformancePointSettings.Current.DestinationServer, PerformancePointSettings.Current.SiteCollectionPath);
                }

                return mHandler;
            }
        }

        private SharepointHandler(string destServer, string siteCollection)
        {
            destinationServer = destServer;
            siteCollectionPath = siteCollection;
        }

        /// <summary>
        /// Returns the current items in the location. We need to clear the items in order to avoid duplicates
        /// </summary>
        /// <param name="destinationServer"></param>
        /// <param name="siteCollectionPath"></param>
        /// <param name="performancePointLocations"></param>
        /// <returns></returns>
        public void PopulateCurrentSharepointItems(List<string> performancePointLocations)
        {
            using (SPSite oSiteCollection = new SPSite(destinationServer))
            {
                using (SPWeb oWebsite = oSiteCollection.OpenWeb(siteCollectionPath))
                {
                    //get the welcome page
                    SPListItem welcomePage = oWebsite.GetFile(oWebsite.RootFolder.WelcomePage).Item;

                    foreach (string location in performancePointLocations)
                    {
                        var list = oWebsite.Lists.TryGetList(location);
                        if (list != null)
                        {
                            //check to see if the welcome page is between these items. These items need to be deleted so we need to change the homepage                        
                            foreach (SPListItem item2 in list.Items)
                            {
                                if (item2.UniqueId.Equals(welcomePage.UniqueId))
                                {
                                    mWelcomePage = oWebsite.RootFolder.WelcomePage;
                                    //we set a temporary default page
                                    SetHomePage(oWebsite, "Pages/ppssample.aspx");
                                    break;
                                }
                                if (item2.ContentType.Name.ToUpper().Contains("performancepoint".ToUpper()))
                                {
                                    sharepointItems.Add(item2);
                                }
                            }
                        }
                    }
                }
            }
        }

        public void RemoveOldItems()
        {
            if (sharepointItems.Count > 0)
            {
                Console.WriteLine("Preparing to remove " + sharepointItems.Count + " items from the deployed locations");
                int i = 0;
                while (sharepointItems.Count > 0)
                {
                    sharepointItems[0].Delete();
                    sharepointItems.RemoveAt(0);
                    i++;
                }
                Console.WriteLine("There were removed " + i.ToString() + " items");
            }
        }

        public void ReStoreHomePage()
        {
            if (!String.IsNullOrEmpty(mWelcomePage))
            {
                using (SPSite oSiteCollection = new SPSite(destinationServer))
                {
                    using (SPWeb oWebsite = oSiteCollection.OpenWeb(siteCollectionPath))
                    {
                        SetHomePage(oWebsite, mWelcomePage);
                    }
                }
            }
        }

        private void SetHomePage(SPWeb oWebsite, string pagePath)
        {
            PublishingWeb publishingWeb = null;

            if (PublishingWeb.IsPublishingWeb(oWebsite))
            {
                publishingWeb = PublishingWeb.GetPublishingWeb(oWebsite);
                SPFile homePageFile = oWebsite.GetFile(pagePath);
                publishingWeb.DefaultPage = homePageFile;
                publishingWeb.Update();
                oWebsite.Update();
            }
        }
    }
}
